package org.weasel.base.actions;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;
import org.weasel.controllers.ApplicationController;
import org.weasel.models.ChangeListener;

public class SaveAction extends Action implements IWorkbenchWindowActionDelegate, ChangeListener {

	private ApplicationController controller;
	private IAction action;
	
	public void dispose() {
		// TODO Auto-generated method stub
		controller.unregisterListener(this);
	}

	public void init(IWorkbenchWindow window) {
		this.setId("org.weasel.base.SaveAction");
		this.controller = ApplicationController.instance();
		this.controller.registerListener(this);
		this.action.setEnabled(false);
	}

	public void run(IAction action) {
		ApplicationController.instance().saveModel();
		this.action.setEnabled(false);
	}

	public void selectionChanged(IAction action, ISelection selection) {
		System.out.println("selectionC");
		this.action = action;
	}

	public void changeHappened() {
		if(this.action!=null)
			this.action.setEnabled(true);
	}

}
